package com.wujunshen.algorithm.leetcode.basic;

/**
 * @author frank woo(吴峻申) <br>
 *     email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2022/7/24 17:19<br>
 */
public class 反转链表II {
  public ListNode reverseBetween(ListNode head, int left, int right) {
    if (head == null) {
      return null;
    }
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode pre = dummy;
    for (int i = 0; i < left - 1; i++) {
      pre = pre.next;
    }

    ListNode start = pre.next;
    ListNode then = start.next;

    for (int i = 0; i < right - left; i++) {
      start.next = then.next;
      then.next = pre.next;
      pre.next = then;
      then = start.next;
    }

    return dummy.next;
  }
}
